let animation = wx.createAnimation({
  duration: 4000, //转盘转动时间
  timingFunction: 'ease-out' //动画效果
})
const app = getApp();
Page({
  data: {
    openid: '',

    CUSTOMER_TEL: '',
    phone: '', //手机号
    rotate: 0, //度数
    turning: false, //是否点击,
    zhunsj: '', //另一个页面带来的数据
    prize: '', //中奖概率
    count: '', //剩余次数
    prizeList: [
      2100, //一等奖 ...
      2400, //二等奖  ...
      2330, //三等奖...
      2610, //参与奖 ...
      2200, //谢谢惠顾 ...
    ],
    //向1006传递的数据
    USER_PRIZE_ID: '',
    txtlist: '', //翻滚的字幕
    flag: true, //设置节流
    modalHidden: true, //新的图片模态框
    ajaj: '', //获取中奖奖项

    jiang_x: '', //奖品等级
    EMPLOYEE_ID: '',

    ad_img: '', //中奖图片


    session_key: '',
  },


  showPopup() {
    this.setData({
      modalHidden: true,

    });
    console.log(11);
  },

  //自定义模态框的取消
  modalCancel: function () {
    let animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-out'
    });
    animation.rotate(0).step();
    this.setData({
      modalHidden: true,
      erdeng: true,
      sandeng: true,
      sideng: true,
      xiexie: true,
      rotate: animation.export(),
    })
    console.log(222);
  },

  //自定义模态框的确定
  queding: function () {
    let animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-out'
    });
    animation.rotate(0).step();
    this.setData({
      modalHidden: true,
      erdeng: true,
      sandeng: true,
      sideng: true,
      xiexie: true,
      rotate: animation.export(),
    })
    console.log(111);
  },


  start: function () {
    let that = this
    // 节流
    if (that.data.flag) {

      that.data.flag = false
      wx.request({
        url: app.globalData.interface+'/ankangdabiao/API_5523_huanledachoujiangchoujiang',
        method: 'POST',
        data: {
          // OPEN_ID: that.data.openid,
          OPEN_ID: app.globalData.openid, //用户OPEN_ID
          session_key: app.globalData.session_key,
          OPEN_UUID: app.globalData.jiami,
          USER_ASSETS_IDCARD: that.data.shuju.data.USER_ASSETS_IDCARD,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res);
          if (res.data.result == 'error') {
            wx.showModal({
              title: '温馨提示',
              content: res.data.data,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后
                  console.log('用户点击确定')
                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            that.setData({
              prize: res.data.data.PRIZE_ID,
            })
            const index_ = res.data.data.PRIZE_ID;
            that.rotate(index_)
          }
        }
      })
      setTimeout(() => {
        that.data.flag = true
      }, 7500)
    }

  },

  // 转盘代码
  rotate: function (index_) {
    // rotate: function () {
    let _this = this;
    // let cat = 60;
    if (!this.data.turning) {
      let rdm = 0; //度数
      rdm = Math.floor(Math.random() * 3600); //最大10圈
      // rdm = 2100; //最大10圈
      //用索引的方法对应角度
      rdm = this.data.prizeList[index_ - 1]; //直接确定转的度数 
      // rdm =2200;//直接确定转的度数 
      animation.rotate(rdm).step();
      this.setData({
        rotate: animation.export(),
        turning: true
      });
      setTimeout(() => {
        this.setData({
          turning: false
        });
        let num = rdm % 360; //转了多少度
        //console.log(num);
        function showModal(str) {
          wx.showModal({
            title: '恭喜抽中',
            content: str,
            success: function () { //点击确定后还原到0度
              let animation = wx.createAnimation({
                duration: 100,
                timingFunction: 'ease-out'
              });
              animation.rotate(0).step();
              _this.setData({
                rotate: animation.export(),
                turning: false
              });
            },
            fail: function (res) {
              console.log(res)
            }
          })
        }



        if (rdm == 2200) {
          // showModal('谢谢惠顾');
          // this.setData({
          //   modalHidden: false
          // })
          wx.showModal({
            title: '温馨提示',
            content: '恭喜抽中5等奖请到安康市内任意网点领取！',
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
                console.log('用户点击确定')

              } else { //这里是点击了取消以后
                console.log('用户点击取消')

              }
            }
          })
        } else if (rdm == 2100) {
          // showModal('一等奖');
          // this.setData({
          //   modalHidden: false
          // })
          wx.showModal({
            title: '温馨提示',
            content: '恭喜抽中1等奖请到安康市内任意网点领取！',
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
                console.log('用户点击确定')

              } else { //这里是点击了取消以后
                console.log('用户点击取消')

              }
            }
          })
        } else if (rdm == 2330) {
          // showModal('三等奖');
          // this.setData({
          //   modalHidden: false
          // })
          wx.showModal({
            title: '温馨提示',
            content: '恭喜抽中3等奖请到安康市内任意网点领取！',
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
                console.log('用户点击确定')

              } else { //这里是点击了取消以后
                console.log('用户点击取消')

              }
            }
          })
        } else if (rdm == 2610) {
          // showModal('参与奖');
          // this.setData({
          //   modalHidden: false
          // })
          wx.showModal({
            title: '温馨提示',
            content: '恭喜抽中4等奖请到安康市内任意网点领取！',
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
                console.log('用户点击确定')


              } else { //这里是点击了取消以后
                console.log('用户点击取消')

              }
            }
          })
        } else if (rdm == 2400) {
          // showModal('二等奖');
          // this.setData({
          //   modalHidden: false
          // })
          wx.showModal({
            title: '温馨提示',
            content: '恭喜抽中2等奖请到安康市内任意网点领取！',
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
                console.log('用户点击确定')

              } else { //这里是点击了取消以后
                console.log('用户点击取消')

              }
            }
          })
        }
      }, 4500)
    }
  },






  onLoad: function (e) {

    console.log(JSON.parse(e.data))
    var shuju = JSON.parse(e.data)
    console.log('1454', shuju);

    if (shuju.data.VALID == 1) {
      wx.showModal({
        title: '温馨提示',
        content: '您已抽中'+shuju.data.PRIZE_ID+'等奖请到安康市内任意网点领取！',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后
            console.log('用户点击确定')

          } else { //这里是点击了取消以后
            console.log('用户点击取消')

          }
        }
      })

      this.setData({
        // phone: app.globalData.phone,
        shuju: shuju,
        session_key: app.globalData.session_key,
      })
  
    }else{


      this.setData({
        // phone: app.globalData.phone,
        shuju: shuju,
        session_key: app.globalData.session_key,
      })
  

    }


    

  },


  onShow() {

    // this.authorization() 


    // this.region()
    // this.setData({
    //   openid: app.globalData.openid,
    // });
    // console.log(this.data.openid);

    var that = this
    wx.login({
      success: function (res) {
        console.log(res);
        if (res.code) { //wx.login获取code。
          console.log(res.code);
          //发起网络请求
          wx.request({
            url: app.globalData.interface+'/post/API_1062_appGetOpenid',
            method: 'GET',
            //向后端发送的数据
            data: {
              code: res.code //将code发送到后台服务器。
            },
            header: {
              "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
            },
            success: res => {
              console.log(res);
              if (res.data.result != 'success') {
                // wx.showToast({
                //   title: '请获取手机号',
                //   duration: 1000
                // })
                console.log(1);
              } else {

                var openid = res.data.openid;
                app.globalData.openid = openid;

                console.log(res.data.openid);
                that.setData({
                  openid: res.data.openid
                })
                // that.scanCod()
              }
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });

  },




})


wx.showShareMenu({
  withShareTicket: true,
  menus: ['shareAppMessage', 'shareTimeline']
})